/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.app.mockexam.theory.user.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.app.mockexam.theory.entity.AppExaminateTheory;
import com.jeesite.modules.app.mockexam.theory.service.AppExaminateTheoryService;
import com.jeesite.modules.app.mockexam.theory.user.entity.TmockexamTheoryUser;
import com.jeesite.modules.app.mockexam.theory.user.service.TmockexamTheoryUserService;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;

/**
 * 理论模拟考试用户记录表Controller
 * @author zpp
 * @version 2020-02-10
 */
@Controller
@RequestMapping(value = "/app/mockexam/theory/user/")
public class TmockexamTheoryUserController{

	@Autowired
	private TmockexamTheoryUserService tmockexamTheoryUserService;
	@Autowired
	private AppExaminateTheoryService appExaminateTheoryService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TmockexamTheoryUser get(String id, boolean isNewRecord) {
		return tmockexamTheoryUserService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("app:mockexam:theory:user:tmockexamTheoryUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(TmockexamTheoryUser tmockexamTheoryUser, Model model) {
		model.addAttribute("tmockexamTheoryUser", tmockexamTheoryUser);
		return "modules/app/mockexam/theory/user/tmockexamTheoryUserList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	public void listData(TmockexamTheoryUser tmockexamTheoryUser, HttpServletRequest request, HttpServletResponse response) {
		String userId=request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		tmockexamTheoryUser.setPage(new Page<>(request, response));
		Page<TmockexamTheoryUser> page = tmockexamTheoryUserService.findPage(tmockexamTheoryUser);
		List<String> theoryList=new ArrayList<String>();
		for(TmockexamTheoryUser ttu:page.getList()){
			String theoryId = ttu.getTheoryId();
			theoryList.add(theoryId);
		}
		Page<AppExaminateTheory> respage=new Page<>(request, response);
		if (theoryList.size()>0){
			AppExaminateTheory appExaminateTheory=new AppExaminateTheory();
			appExaminateTheory.getSqlMap().getWhere().and("id", QueryType.IN, theoryList);
			List<AppExaminateTheory> findList = appExaminateTheoryService.findList(appExaminateTheory);
			respage.setList(findList);
		}else{
			respage.setList(null);
		}
		respage.setCount(page.getCount());
		JSONObject parseObject = JSONObject.parseObject(JsonMapper.toJson(respage));
		AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);

	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("app:mockexam:theory:user:tmockexamTheoryUser:view")
	@RequestMapping(value = "form")
	public String form(TmockexamTheoryUser tmockexamTheoryUser, Model model) {
		model.addAttribute("tmockexamTheoryUser", tmockexamTheoryUser);
		return "modules/app/mockexam/theory/user/tmockexamTheoryUserForm";
	}

	/**
	 * 保存理论模拟考试用户记录表
	 */
	@PostMapping(value = "save")
	public void save(TmockexamTheoryUser entity,HttpServletRequest request,HttpServletResponse response) {
		String theoryId = entity.getTheoryId();
		if(StringUtils.isBlank(theoryId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "理论记录标识");
			return;
		}
		String userId=entity.getUserId();
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		String postId = entity.getPostId();
		if(StringUtils.isBlank(postId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "岗位标识");
			return;
		}
		List<TmockexamTheoryUser> findList = tmockexamTheoryUserService.findList(entity);
		if(findList!=null&&findList.size()>0){
			entity = findList.get(0);			
		}else{
			entity.setType(1);
		    tmockexamTheoryUserService.save(entity);
		}
		JSONObject json=JSONObject.parseObject(JsonMapper.toJson(entity));
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
		return;
	}
	
	/**
	 * 停用理论模拟考试用户记录表
	 */
	@RequestMapping(value = "disable")
	public void disable(TmockexamTheoryUser tmockexamTheoryUser) {
		tmockexamTheoryUser.setStatus(TmockexamTheoryUser.STATUS_DISABLE);
		tmockexamTheoryUserService.updateStatus(tmockexamTheoryUser);
	}
	
	/**
	 * 启用理论模拟考试用户记录表
	 */
	@RequestMapping(value = "enable")
	public void enable(TmockexamTheoryUser tmockexamTheoryUser) {
		tmockexamTheoryUser.setStatus(TmockexamTheoryUser.STATUS_NORMAL);
		tmockexamTheoryUserService.updateStatus(tmockexamTheoryUser);
	}
	
	/**
	 * 删除理论模拟考试用户记录表
	 */
	@RequestMapping(value = "delete")
	public void delete(TmockexamTheoryUser tmockexamTheoryUser) {
		tmockexamTheoryUserService.delete(tmockexamTheoryUser);
	}
	
}